<?php

namespace App\Http\Controllers;


trait MessagePacket
{
    protected $parket = [];

    function  __construct()
    {
        $this->parket['data'] = [];
        $this->parket['status'] = ['code' => 0, 'message' => ''];
    }

    function setData($data)
    {
        if($data===true) $data = 'true';
        if($data===false) $data ='false';

        $this->parket['data'] = $data;
        return $this;
    }

    function setCode($code)
    {
        $this->parket['status']['code'] = $code;
        return $this;
    }

    function setMessage($message)
    {
        $this->parket['status']['message'] = $message;
        return $this;
    }

    /**
     * 获取消息包
     * @return array
     */
    function getPacket()
    {
        return $this->parket;
    }

    function success($data,$message = "请求成功")
    {
        $this->setData($data)->setCode(0)->setMessage($message);
        return $this->getPacket();
    }

    function fail($data, $message='请求失败')
    {
        $this->setData($data)->setCode(1)->setMessage($message);
        return $this->getPacket();
    }



}
